#!/usr/bin/python3
# -*- coding: utf8 -*-

# Copyright (c) 2021 Baidu, Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Example: General Molmer-Sorensen gate
Please visit https://quanlse.baidu.com/#/doc/tutorial-general-MS-gate for more details about this example.
"""

import numpy as np
from math import pi

from Quanlse.Utils.Functions import basis
from Quanlse.Utils.Functions import computationalBasisList
from Quanlse.Utils.Plot import plotBarGraph
from Quanlse.Utils import Plot
from Quanlse import Define
from Quanlse.remoteOptimizer import remoteIonGeneralMS as runGeneralIonMS


# Your token:
# Please visit http://quantum-hub.baidu.com
Define.hubToken = ''

# --------------------------------
# Define the system information.
# --------------------------------

# System qubit number.
ionNumber = 7

# System ion mass.
mass = 171

# XY, Z direction trap potential frequency.
omegaXY = 2 * pi * 2e6
omegaZ = 2 * pi * 0.2e6

# Phonon mode.
phononMode = "transverse"

args1 = (ionNumber, mass, omegaXY, omegaZ, phononMode)

# --------------------------------
# Define the gate information.
# --------------------------------

# Total time of quantum gate.
tg = 200

# The laser detuning, usually related with gate time. but can tuning around 2 * pi / tg.
mu = 2 * pi / tg

# The pulse sequence slice number, usually N > 3 * ionNumber.
N = 35

# Sampling period.
dt = tg / N

# Combine the parameters in list.
args2 = (N, dt, mu)

# Define two gate pairs of general Molmer-Sorensen gate.
gatePair = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]]

# ----------------------------------------
# Run the simulation and show the results.
# ----------------------------------------

# Run the simulation.
res, ureal = runGeneralIonMS(gatePair, args1=args1, args2=args2)

pulse = res['pulse_list']

# Choose the pulse sequence of ionpair.
ionpair = gatePair.index([0, 1])

# Plot the laser pulse.
Plot.plotPulse([np.arange(N) * dt * (N+1) / N], [pulse[ionpair]],
               title=[f'Pulse for ionpair={gatePair[ionpair]} '],
               xLabel=r'Time ($\mu$s)', yLabel=['Rabi frequency (a.u)'], color=['blue'])

# Print the result of simulation.
print(ureal)

# Print the infidelity of general MS gate.
print(f"The parallel Molmer-Sorensen gate infidelity:\n {res['infidelity']}")
print(f"The pulse residual error:\n {res['laser_residual_error']}")

# Plot the population.
finalState = (ureal @ np.array(basis(16, 0))).T[0]
population = [abs(state ** 2) for state in finalState]
basis = computationalBasisList(4, 2)
plotBarGraph(basis, population, "Population of a 4-Qubits GHZ state generated by General MS gate",
             "Computational Basis", "Population")
